package nongchang.kuaijiecaifu.com.ncapp_android.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;


import butterknife.BindView;
import butterknife.ButterKnife;
import nongchang.kuaijiecaifu.com.ncapp_android.R;

public class LoadingLayout extends RelativeLayout {

    ViewGroup layoutContent;

    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.layout_loading)
    LinearLayout layoutLoading;
    @BindView(R.id.layout_error)
    LinearLayout layoutError;

    @BindView(R.id.img_empty)
    ImageView imgEmpty;
    @BindView(R.id.txt_empty)
    TextView txtEmpty;

    @BindView(R.id.progress_loading)
    ProgressBar progressLoading;
    @BindView(R.id.txt_loading)
    TextView txtLoading;

    @BindView(R.id.img_error)
    ImageView imgError;
    @BindView(R.id.txt_error)
    TextView txtError;
    @BindView(R.id.txt_error_desc)
    TextView txtErrorDesc;

    public LoadingLayout(Context context) {
        super(context);
        initWithContext(context, null);
    }

    public LoadingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWithContext(context, attrs);
    }

    public LoadingLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initWithContext(context, attrs);
    }


    Drawable emptyImage = null, errorImage = null;
    String emptyText = "", loadingText = "", errorText = "", errorDesc = "";
    Integer contentLayoutId = 0;

    private void initWithContext(Context context, AttributeSet attrs) {

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.LoadingLayout);

        try {
            emptyImage = a.getDrawable(R.styleable.LoadingLayout_emptyImage);
            emptyText = a.getString(R.styleable.LoadingLayout_emptyText);
            loadingText = a.getString(R.styleable.LoadingLayout_loadingText);
            errorImage = a.getDrawable(R.styleable.LoadingLayout_errorImage);
            errorText = a.getString(R.styleable.LoadingLayout_errorText);
            errorDesc = a.getString(R.styleable.LoadingLayout_errorDesc);
            contentLayoutId = a.getResourceId(R.styleable.LoadingLayout_contentLayoutId, NO_ID);
        } finally {
            a.recycle();
        }

        inflate(context, R.layout.widget_empty, this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.bind(this, this);

        if (emptyImage != null) {
            imgEmpty.setImageDrawable(emptyImage);
        }
        if (errorImage != null) {
            imgError.setImageDrawable(errorImage);
        }
        txtEmpty.setText(emptyText);
        txtLoading.setText(loadingText);
        txtError.setText(errorText);
        txtErrorDesc.setText(errorDesc);

        layoutContent = (ViewGroup)findViewById(contentLayoutId);

        showLoading();

    }


    public LoadingLayout emptyImage(int resId) {
        imgEmpty.setImageResource(resId);
        return this;
    }
    public LoadingLayout errorImage(int resId) {
        imgError.setImageResource(resId);
        return this;
    }

    public LoadingLayout emptyText(String value) {
        txtEmpty.setText(value);
        return this;
    }
    public LoadingLayout loadingText(String value) {
        txtLoading.setText(value);
        return this;
    }
    public LoadingLayout errorText(String value) {
        txtEmpty.setText(value);
        return this;
    }

    public LoadingLayout errorDesc(String value) {
        txtErrorDesc.setText(value);
        return this;
    }

    public LoadingLayout showLoading() {
        layoutEmpty.setVisibility(GONE);
        layoutError.setVisibility(GONE);
        layoutLoading.setVisibility(VISIBLE);
        layoutContent.setVisibility(GONE);
        return this;
    }

    public LoadingLayout showEmpty() {
        layoutEmpty.setVisibility(VISIBLE);
        layoutError.setVisibility(GONE);
        layoutLoading.setVisibility(GONE);
        layoutContent.setVisibility(GONE);
        return this;
    }

    public LoadingLayout showError() {
        layoutEmpty.setVisibility(GONE);
        layoutError.setVisibility(VISIBLE);
        layoutLoading.setVisibility(GONE);
        layoutContent.setVisibility(GONE);
        return this;
    }
    public LoadingLayout showContent() {
        layoutEmpty.setVisibility(GONE);
        layoutError.setVisibility(GONE);
        layoutLoading.setVisibility(GONE);
        layoutContent.setVisibility(VISIBLE);
        return this;
    }

}
